<?php
namespace model;

/**
 * Model class for handling address records
 * @author milena
 *
 */
class Address
{
    /**
     * Location of the CSV file 
     * @var string
     */
    const storageLocation = 'data/addresses.csv';

    /**
     * Get all addresses
     * @return array
     */
    public static function getAddresses()
    {
        $addresses = [];
        $file = fopen(self::storageLocation, 'r');
        while (($line = fgetcsv($file)) !== FALSE) {
            $addresses[] = [
                'name' => $line[0],
                'phone' => $line[1],
                'street' => $line[2]
            ];
        }
        
        fclose($file);
        
        return $addresses;
    }

    /**
     * Retrieve by ID, where ID is just the order number in the CSV
     * @param Integer $id
     * @return array
     */
    public static function getAddressById($id)
    {
        $addresses = self::getAddresses();
        $record = null;
        if (isset($addresses[$id])) {
            $record = $addresses[$id];
        }
        return $record;
    }
    
    /**
     * Create new record
     * @param array $params
     * @return array record
     */
    public static function addAddress(Array $params)
    {
        if (! self::isValidAddress($params)) {
            return;
        }
        $addresses = self::getAddresses();
        $addresses[] = [
            "name" => $params['name'],
            "phone" => $params['phone'],
            "street" => $params['street']
        ];
        
        $addresses = self::saveData($addresses);
        
        // $addresses = self::getAddresses(); // we could use this to make sure data is written
        
        $lastRecord = $addresses[count($addresses) - 1];
        
        return $lastRecord;
    }

    /**
     * Delete record
     * @param integer $id
     * @return boolean
     */
    public static function deleteAddress($id)
    {
        $isDeleted = false;
        $addresses = self::getAddresses();
        $oldCount = count($addresses);
        if (isset($addresses[$id - 1])) {
            unset($addresses[$id - 1]);
            $newAddresses = self::saveData($addresses);
            if ($oldCount == count($newAddresses) + 1) {
                $isDeleted = true;
            }
        }
        return $isDeleted;
    }

    /**
     * Update record
     * @param integer $id
     * @param array $params
     */
    public static function updateAddress($id, $params)
    {
        if (! self::isValidAddress($params)) {
            return;
        }
        $addresses = self::getAddresses();
        $addresses[$id] = [
            'name' => $params['name'],
            'phone' => $params['phone'],
            'street' => $params['street']
        ];
        return self::saveData($addresses);
    }

    /**
     * Check if an address is valid
     * TODO create object class for address record
     * @param Array $params
     * @return boolean
     */
    private static function isValidAddress($params)
    {
        return ! in_array("", $params);
    }

    /**
     * Save data to CSV file
     * @param Array $addresses
     * @return Array
     */
    private static function saveData($addresses)
    {
        $file = fopen(self::storageLocation, 'w');
        foreach ($addresses as $fields) {
            fputcsv($file, $fields);
        }
        fclose($file);
        return $addresses;
    }
}
